﻿using Dapper;
using Microsoft.Extensions.Configuration;
using PcProject.Domain.Models.Entities;
using PcProject.Infrastructure.Repository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DomainService
{
    public class ProductBargain: DapperRepositoryBase<LaunchBargain>, IProductBargain
    {
        //构造函数依赖注入
        public IConfiguration _configuration;
        public BargainRepository _bargainRepository;
        //函数依赖注入
        public ProductBargain(IConfiguration configuration, BargainRepository bargainRepository)
        {
            this._configuration = configuration;
            _bargainRepository = bargainRepository;
            //连接字符串
            this.ConnStr = _configuration.GetConnectionString("PcMiniConnStr");
            SimpleCRUD.SetDialect(SimpleCRUD.Dialect.MySQL);
            Conn = new MySql.Data.MySqlClient.MySqlConnection(this.ConnStr);
        }

        /// <summary>
        /// 随机减价方法
        /// </summary>
        /// <param name="ran"></param>
        /// <param name="minValue"></param>
        /// <param name="maxValue"></param>
        /// <returns></returns>
        public double NextDouble(Random ran, double minValue, double maxValue)
        {
            return ran.NextDouble() * (maxValue - minValue) + minValue;
        }

        /// <summary>
        /// 随机砍价
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int ProBargain(int id)
        {
            //开启事务
            Conn.Open();
            var tran = this.Conn.BeginTransaction();
            try
            {
                //生成随机数
                Random random = new Random();
                //根据id查询发起砍价信息
                LaunchBargain launch = this.Conn.Get<LaunchBargain>(id);
                //获取砍价信息
                List<Bargain> bargains = this.Conn.GetList<Bargain>().ToList();
                //随机砍价最大数最小数
                decimal m = bargains.Where(x => x.BargainId.Equals(id)).FirstOrDefault().MaxMargain;
                decimal n = bargains.Where(x => x.BargainId.Equals(id)).FirstOrDefault().MinBargain;
                //调用随机方法
                double peice = NextDouble(random, Convert.ToDouble(n), Convert.ToDouble(m));
                //定义全局变量
                int price;
                //判断价格是否大于底价
                if (launch.EndPrice >= bargains.FirstOrDefault().MinPrice)
                {
                    //计算随机砍价之后的价格
                    launch.EndPrice = launch.EndPrice - Convert.ToDecimal(peice);
                    //修改价格
                    price = _bargainRepository.Upt(launch);
                }
                else
                {
                    launch.EndPrice = bargains.FirstOrDefault().MinPrice;
                    price = _bargainRepository.Upt(launch);
                }
                //提交
                tran.Commit();
                return price;
                
            }
            catch (Exception)
            {
                //回滚事务
                tran.Rollback();
                throw;
            }
        }

    }
}
